#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#define MAXN 505
#define MAXM 100005
using namespace std;

long long times[MAXN];
vector<int> dependencies[MAXN];
long long minfinishtime[MAXN];

long long get_minfinishtime(int i){
    if (minfinishtime[i] == -1){
        long long start = 0;
        for (int j=0;j<dependencies[i].size();j++){
            start = max(start, get_minfinishtime(dependencies[i][j]));
        }
        minfinishtime[i] = start + times[i];
    }
    return minfinishtime[i];
}

int main()
{
    freopen("msched.txt","r", stdin);
    int n,m;
    scanf("%d%d", &n, &m);
    for (int i=0;i<n;i++){
        scanf("%lld", &times[i]);
        minfinishtime[i] = -1;
    }
    for (int i=0;i<m;i++){
        int a,b;
        scanf("%d%d", &a, &b);
        a--; b--;
        dependencies[b].push_back(a);
    }
    long long ans = 0;
    for (int i=0;i<n;i++)
        ans = max(ans, get_minfinishtime(i));
    printf("%d\n", ans);
    return 0;
}
